.PHONY: all clean clean_all requirements

#################################################################################
# GLOBALS                                                                       #
#################################################################################

PROJECT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
PROJECT_NAME = demoracy_origins

#################################################################################
# COMMANDS                                                                      #
#################################################################################

## Delete all Rout and Stata log files
clean:
	find . -type f -name "*.Rout" -delete
	find . -type f -name "*.log" -delete

## Delete all Rout, downloaded files, and generated figures
clean_all: clean clean_ngrams

clean_ngrams:
	cd data; \
	rm -f ./1-000* totalcounts-1 democracy.txt republic.txt

clean_figures:
	rm Rplots.pdf
	cd output; \
	rm -f *.tiff

#################################################################################
# PROJECT TARGETS                                                               #
#################################################################################

## Everything
all: tables figures maps

## Figures
figures: figure_2_1 figure_2_3 figure_4_2 figure_6_2 figure_6_3 figure_7_1 figure_7_2 figure_7_3 figure_7_4 figure_9_2 figure_9_3 figure_11_2 figure_13_1 figure_16_1 figure_16_2 appendix_e

figure_2_1: code/chapter_2/figure_2_1.R
	R CMD BATCH --no-save --no-restore $<

figure_2_3: code/chapter_2/figure_2_3.R
	R CMD BATCH --no-save --no-restore $<

figure_4_2: code/chapter_4/table_4_2.do
	stata -b $<

figure_6_2: code/chapter_6/figure_6_2.do
	stata -b $<

figure_6_3: code/chapter_6/figure_6_3.R
	R CMD BATCH --no-save --no-restore $<

figure_7_1: code/chapter_7/figure_7_1.R
	R CMD BATCH --no-save --no-restore $<

figure_7_2: code/chapter_7/figure_7_2.R
	R CMD BATCH --no-save --no-restore $<

figure_7_3: code/chapter_7/figure_7_3.R
	R CMD BATCH --no-save --no-restore $<

figure_7_4: code/chapter_7/figure_7_4.R
	R CMD BATCH --no-save --no-restore $<

figure_9_2: code/chapter_9/figure_9_2.R
	R CMD BATCH --no-save --no-restore $<

figure_9_3: code/chapter_9/figure_9_3.R
	R CMD BATCH --no-save --no-restore $<

figure_11_2: code/chapter_11/figure_11_2.R
	R CMD BATCH --no-save --no-restore $<

figure_13_2: code/chapter_13/figure_13_2.R
	R CMD BATCH --no-save --no-restore $<

figure_16_1: code/chapter_16/figure_16_1.R
	R CMD BATCH --no-save --no-restore $<

figure_16_2: code/chapter_16/figure_16_2.R
	R CMD BATCH --no-save --no-restore $<

appendix_e: code/appendices/appendix_e.R
	R CMD BATCH --no-save --no-restore $<

## Tables
tables: table_2_1o table_4_5 table_4_6 table_5_1 tables_6 table_7_2 table_7_3 table_7_4 table_7_5 table_7_6 table_7_7_and_8 table_9_1 table_10_1 table_10_3 table_10_4 table_10_5 table_11_1 table_11_2 table_11_3 table_11_4 table_11_5 table_11_6 tables_chapter_12 tables_chapter_13 tables_chapter_14 appendix_a appendix_b

table_2_1: code/chapter_2/table_2_1.do
	stata -b $<

table_4_5: code/chapter_4/table_4_5.R
	R CMD BATCH --no-save --no-restore $<

table_4_6: code/chapter_4/table_4_6.do
	stata -b $<

table_5_1: code/chapter_5/table_5.do
	stata -b $<

table_6: code/chapter_6/chapter_6_tables.do
	stata -b $<

table_7_2: code/chapter_7/table_7_2.do
	stata -b $<

table_7_3: code/chapter_7/table_7_3.do
	stata -b $<

table_7_4: code/chapter_7/table_7_4.do
	stata -b $<

table_7_5: code/chapter_7/table_7_5.R
	R CMD BATCH --no-save --no-restore $<

table_7_6: code/chapter_7/table_7_6.do
	stata -b $<

table_7_7_and_8: code/chapater_7/table_7_7_8.R
	R CMD BATCH --no-save --no-restore $<

table_9_1: code/chapter_9/table_9_1.do
	stata -b $<

table_10_1: code/chapter_10/table_10_1.do
	stata -b $<

table_10_3: code/chapter_10/table_10_3.do
	stata -b $<

table_10_4: code/chapter_10/table_10_4.do
	stata -b $<

table_10_5: code/chapter_10/table_10_5.do
	stata -b $<

table_11_1: code/chapter_11/table_11_1.do
	stata -b $<

table_11_2: code/chapter_11/table_11_2.do
	stata -b $<

table_11_3: code/chapter_11/table_11_3.do
	stata -b $<

table_11_4: code/chapter_11/table_11_4.do
	stata -b $<

table_11_5: code/chapter_11/table_11_5.do
	stata -b $<

table_11_6: code/chapter_11/table_11_6.do
	stata -b $<

tables_chapter_12: code/chapter_12/tables_chapter_12.do
	stata -b $<

tables_chapter_13: code/chapter_13/tables_chapter_13.do
	stata -b $<

tables_chapter_14: code/chapter_14/tables_chapter_14.do
	stata -b $<

appendix_a: code/appendices/appendix_a.do
	stata -b $<

appendix_b: code/appendices/appendix_b.do
	stata -b $<

## Maps
maps: map_2_1 map_2_2 map_9_1

map_2_1: code/chapter_2/map_2_1.R
	R CMD BATCH --no-save --no-restore $<

map_2_2: code/chapter_2/map_2_2.R
	R CMD BATCH --no-save --no-restore $<

map_9_1: code/chapter_2/map_9_1.R
	R CMD BATCH --no-save --no-restore $<

#################################################################################
# Self Documenting Commands                                                     #
#################################################################################

.DEFAULT_GOAL := help

# Inspired by <http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html>
# sed script explained:
# /^##/:
# 	* save line in hold space
# 	* purge line
# 	* Loop:
# 		* append newline + line to hold space
# 		* go to next line
# 		* if line starts with doc comment, strip comment character off and loop
# 	* remove target prerequisites
# 	* append hold space (+ newline) to line
# 	* replace newline plus comments by `---`
# 	* print line
# Separate expressions are necessary because labels cannot be delimited by
# semicolon; see <http://stackoverflow.com/a/11799865/1968>
.PHONY: help
help:
	@echo "$$(tput bold)Available rules:$$(tput sgr0)"
	@echo
	@sed -n -e "/^## / { \
		h; \
		s/.*//; \
		:doc" \
		-e "H; \
		n; \
		s/^## //; \
		t doc" \
		-e "s/:.*//; \
		G; \
		s/\\n## /---/; \
		s/\\n/ /g; \
		p; \
	}" ${MAKEFILE_LIST} \
	| LC_ALL='C' sort --ignore-case \
	| awk -F '---' \
		-v ncol=$$(tput cols) \
		-v indent=19 \
		-v col_on="$$(tput setaf 6)" \
		-v col_off="$$(tput sgr0)" \
	'{ \
		printf "%s%*s%s ", col_on, -indent, $$1, col_off; \
		n = split($$2, words, " "); \
		line_length = ncol - indent; \
		for (i = 1; i <= n; i++) { \
			line_length -= length(words[i]) + 1; \
			if (line_length <= 0) { \
				line_length = ncol - indent - length(words[i]) - 1; \
				printf "\n%*s ", -indent, " "; \
			} \
			printf "%s ", words[i]; \
		} \
		printf "\n"; \
	}' \
	| more $(shell test $(shell uname) = Darwin && echo '--no-init --raw-control-chars')
